<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

/**
 * 用户操作日志管理 
 */

class AnalysisController extends BaseController {

	public function index(){
		$psize = 7;
		// $psize = I('get.psize', $this->$m_psize,'intval');
		$ids = I('get.ids',-1,'intval');
		$s_begintime = I('get.begintime','','trim');
        $s_endtime = I('get.endtime','','trim');
		if( !$psize )
            $psize = $this->m_psize;
        else if( $psize > 7 )
            $psize = $this->m_psize;

		$res = array('rcode'=>1,'data'=>NULL, 'msg'=> 'OK');
		$Sector = M('Sector');
		$User = M('User');
		$CustomerXufei = M("customer_xufei");
		$Payrecord = M('pay_record');
		$Role = M('role');
		$wheres = array();
		
		$map1['is_deleted'] = array('neq',1);
        $array = $Sector->field("sector_id,sector_name,parent_sector") -> where($map1)->select();
		$arr = list_to_sector($array,$this->m_user['sector_id']);
			/*$new_arr:普通用户、经理：sector_id；总监、总办、admin：自己以及自己下属部门所有id*/
        foreach ($arr as $key => $value) {
            $new_arr[] = $value['sector_id'];
        }
        $map2['sector_id'] = array('in',$new_arr);
		$map2['is_deleted'] = array('neq',1);
        $user_list = $User->field('user_id,nickname,sector_id') -> where($map2) ->select();
        $i = -1;
		foreach ($user_list as $ukey => $uvalue) {
		//$analysis_list1存储员工资源分配情况
			$i++;
			//分配资源
			$map3['user_id'] = array('eq',$uvalue['user_id']);
			if($s_begintime && $s_endtime)
				$map3['start_time'] = array('between',[$s_begintime,$s_endtime]);
			$resou_num = $CustomerXufei ->where($map3) ->count();
			$analysis_list1[$i]['resou_num'] = $resou_num ? $resou_num : 0;	

			//显示列表中用户昵称
			$analysis_list1[$i]['nickname'] = $uvalue['nickname'];

			//已建仓：1 持仓中、2 止盈、-1 亏损
			$map4['user_id'] = array('eq',$uvalue['user_id']);
			$map4['account_state'] = array('in',[1,2,-1]);
			if($s_begintime && $s_endtime)
				$map4['start_time'] = array('between',[$s_begintime,$s_endtime]);
			$jiancang_ok = $CustomerXufei ->where($map4)->count();
			$analysis_list1[$i]['jiancang_ok'] = $jiancang_ok ? $jiancang_ok : 0;

			//未建仓：0 待荐股
			$map4['account_state'] = array('eq',0);
			if($s_begintime && $s_endtime)
				$map4['start_time'] = array('between',[$s_begintime,$s_endtime]);
			$jiancang_no = $CustomerXufei ->where($map4)->count();
			$analysis_list1[$i]['jiancang_no'] = $jiancang_no ? $jiancang_no : 0;

			//止盈
			$map4['account_state'] = array('eq',2);
			if($s_begintime && $s_endtime)
				$map4['start_time'] = array('between',[$s_begintime,$s_endtime]);
			$zhiying = $CustomerXufei ->where($map4)->count();
			$analysis_list1[$i]['zhiying'] = $zhiying ? $zhiying : 0;

			//成交
			$maps['customer_sales'] = array('eq',$uvalue['user_id']);
			$maps['audit_res'] = array('eq',1);	//审核结果:1 已经审核成功
			$maps['pay_type'] = array('eq',2);	//付款类型：2、续费
			if($s_begintime && $s_endtime)
				$maps['create_time'] = array('between',[$s_begintime,$s_endtime]);
			$deal_mount = $Payrecord->field('payrecord_id')
								->where($maps)
								->select();
			foreach ($deal_mount as $key => $value) {
				if($value['payrecord_id'])
					$deal_mounts[$i] += 1;
				else $deal_mounts[$i] = 0;
			}
			$analysis_list1[$i]['deal_mounts'] = $deal_mounts[$i] ? $deal_mounts[$i] : 0;

			//成交率
			if($analysis_list1[$i]['resou_num']&&$analysis_list1[$i]['deal_mounts'])
				$analysis_list1[$i]['dealret'] = strval(round($analysis_list1[$i]['deal_mounts']/$analysis_list1[$i]['resou_num'],4)*100).'%';
			else $analysis_list1[$i]['dealret'] = 0;

			//失联
			$map5['user_id'] = array('eq',$uvalue['user_id']);
			$map5['is_new'] = array('eq',-1);
			if($s_begintime && $s_endtime)
				$map5['start_time'] = array('between',[$s_begintime,$s_endtime]);
			$shilian = $CustomerXufei ->where($map5)->count();
			$analysis_list1[$i]['shilian'] = $shilian ? $shilian : 0;		

			//投诉
			$map6['customer_xufei.user_id'] = array('eq',$uvalue['user_id']);
			$map6['customer_complain.is_delete'] = array('neq',1);
			if($s_begintime && $s_endtime)
				$map6['customer_xufei.start_time'] = array('between',[$s_begintime,$s_endtime]);
			$complain_num = $CustomerXufei
							->field('customer_complain.complain_id') 
							->join('customer_complain on customer_complain.customer_id=customer_xufei.customer_id')
							->where($map6)
							->count();
			$analysis_list1[$i]['complain_num'] = $complain_num ? $complain_num : 0;		

			//新单数
			$mapp['customer_sales'] = array('eq',$uvalue['user_id']);
			$mapp['pay_type'] = array('eq',1);	//付款类型:1 新单
			$mapp['audit_res'] = array('eq',1);	//审核结果:1 已经审核成功
			if($s_begintime && $s_endtime)
				$mapp['create_time'] = array('between',[$s_begintime,$s_endtime]);
			$xindan = $Payrecord ->field('payrecord_id')
						->where($mapp)
						->select();
			foreach ($xindan as $key => $value) {
				if($value['payrecord_id'])
					$xindans[$i] += 1;
				else $xindans[$i] = 0;
			}
			$analysis_list1[$i]['xindan'] = $xindans[$i] ? $xindans[$i] : 0;

			//新单业绩
			$map7['customer_sales'] = array('eq',$uvalue['user_id']);
			$map7['pay_type'] = array('eq',1);	//付款类型:1 新单
			$map7['audit_res'] = array('eq',1);	//审核结果:1 已经审核成功
			if($s_begintime && $s_endtime)
				$map7['create_time'] = array('between',[$s_begintime,$s_endtime]);
			$xindan_paymount = $Payrecord->field('pay_mount')
								->where($map7)
								->select();
			foreach ($xindan_paymount as $key => $value) {
				if($value['pay_mount'])
					$xindan_pay_total[$i] += $value['pay_mount'];
				else $xindan_pay_total[$i] = 0;
			}	
			$analysis_list1[$i]['xindan_pay_total'] = $xindan_pay_total[$i] ? $xindan_pay_total[$i] : 0;	

			//续费业绩
			$map8['customer_sales'] = array('eq',$uvalue['user_id']);
			$map8['pay_type'] = array('eq',2);	//付款类型:2 续费
			$map8['audit_res'] = array('eq',1);	//审核结果:1 已经审核成功
			if($s_begintime && $s_endtime)
				$map8['create_time'] = array('between',[$s_begintime,$s_endtime]);
			$xufei_paymount = $Payrecord->field('pay_mount')
								->where($map8)
								->select();
			foreach ($xufei_paymount as $key => $value) {
				if($value['pay_mount'])
					$xufei_pay_total[$i] += $value['pay_mount'];
				else $xufei_pay_total[$i] = 0;
			}	
			$analysis_list1[$i]['xufei_pay_total'] = $xufei_pay_total[$i] ? $xufei_pay_total[$i] : 0;

			//退款
			$map9['customer_sales'] = array('eq',$uvalue['user_id']);
			$map9['pay_type'] = array('eq',3);	//付款类型:3 退款
			$map9['audit_res'] = array('eq',1);	//审核结果:1 已经审核成功
			if($s_begintime && $s_endtime)
				$map9['create_time'] = array('between',[$s_begintime,$s_endtime]);
			$tuikuan_paymount = $Payrecord->field('pay_mount')
								->where($map9)
								->select();
			foreach ($tuikuan_paymount as $key => $value) {
				if($value['pay_mount'])
					$tuikuan_pay_total[$i] += $value['pay_mount'];
				else $tuikuan_pay_total[$i] = 0;
			}	
			$analysis_list1[$i]['tuikuan_pay_total'] = -$tuikuan_pay_total[$i] ? -$tuikuan_pay_total[$i] : 0;

			//总资源
			$map10['user_id'] = array('eq',$uvalue['user_id']);
			$resou_total = $CustomerXufei ->where($map10) ->count();
			$analysis_list1[$i]['resou_total'] = $resou_total ? $resou_total : 0;

			//总业绩
			$map11['customer_sales'] = array('eq',$uvalue['user_id']);
			$map11['audit_res'] = array('eq',1);		//审核结果:1 已经审核成功
			if($s_begintime && $s_endtime)
				$map11['create_time'] = array('between',[$s_begintime,$s_endtime]);
			$eval_mount = $Payrecord->field('pay_mount')
								->where($map11)
								->select();
			foreach ($eval_mount as $key => $value) {
				if($value['pay_mount'])
					$eval_mount_total[$i] += $value['pay_mount'];
				else $eval_mount_total[$i] = 0;
			}
			$analysis_list1[$i]['eval_mount_total'] = $eval_mount_total[$i] ? $eval_mount_total[$i] : 0;

			//单均产值=总业绩/分配资源
			if($analysis_list1[$i]['resou_num'])
				$analysis_list1[$i]['record_aver'] = intval($analysis_list1[$i]['eval_mount_total']/$analysis_list1[$i]['resou_num']);
			else $analysis_list1[$i]['record_aver'] = 0;

			//客户资金分布:0-10W
			$map12['customer.mount'] = array(array('LT',10),array('GT',0));	// <10W >0
			$map12['customer_xufei.user_id'] = array('eq',$uvalue['user_id']);
			if($s_begintime && $s_endtime)
				$map12['customer_xufei.start_time'] = array('between',[$s_begintime,$s_endtime]);
			$customer_mount1 = $CustomerXufei
							->field('customer.mount')
							->join('customer on customer_xufei.customer_id=customer.customer_id')
							->where($map12)
							->select();
			foreach ($customer_mount1 as $key => $value) {
				if($value['mount']<10)
					$customer_mount1_total[$i] += 1;
				else $customer_mount1_total[$i] = 0;
			}	
			$analysis_list1[$i]['customer_mount1_total'] = $customer_mount1_total[$i] ? $customer_mount1_total[$i] : 0;

			//客户资金分布:10-50W
			$map13['customer.mount'] = array(array('EGT',10),array('LT',50));	// >=10W <50W
			$map13['customer_xufei.user_id'] = array('eq',$uvalue['user_id']);
			if($s_begintime && $s_endtime)
				$map13['customer_xufei.start_time'] = array('between',[$s_begintime,$s_endtime]);
			$customer_mount2 = $CustomerXufei
							->field('customer.mount')
							->join('customer on customer_xufei.customer_id=customer.customer_id')
							->where($map13)
							->select();
			foreach ($customer_mount2 as $key => $value) {
				if($value['mount'])
					$customer_mount2_total[$i] += 1;
				else $customer_mount2_total[$i] = 0;
			}	
			$analysis_list1[$i]['customer_mount2_total'] = $customer_mount2_total[$i] ? $customer_mount2_total[$i] : 0;

			//客户资金分布:50-100W
			$map14['customer.mount'] = array(array('EGT',50),array('LT',100));	// >=50W <100W
			$map14['customer_xufei.user_id'] = array('eq',$uvalue['user_id']);
			if($s_begintime && $s_endtime)
				$map14['customer_xufei.start_time'] = array('between',[$s_begintime,$s_endtime]);
			$customer_mount3 = $CustomerXufei
							->field('customer.mount')
							->join('customer on customer_xufei.customer_id=customer.customer_id')
							->where($map14)
							->select();
			foreach ($customer_mount3 as $key => $value) {
				if($value['mount'])
					$customer_mount3_total[$i] += 1;
				else $customer_mount3_total[$i] = 0;
			}	
			$analysis_list1[$i]['customer_mount3_total'] = $customer_mount3_total[$i] ? $customer_mount3_total[$i] : 0;

			//客户资金分布:>=100W
			$map15['customer_xufei.user_id'] = array('eq',$uvalue['user_id']);
			$map15['customer.mount'] = array('EGT',100);
			if($s_begintime && $s_endtime)
				$map15['customer_xufei.start_time'] = array('between',[$s_begintime,$s_endtime]);
			$customer_mount4 = $CustomerXufei
							->field('customer.mount')
							->join('customer on customer_xufei.customer_id=customer.customer_id')
							->where($map15)
							->select();
			foreach ($customer_mount4 as $key => $value) {
				if($value['mount'])
					$customer_mount4_total[$i] += 1;
				else $customer_mount4_total[$i] = 0;
			}
			$analysis_list1[$i]['customer_mount4_total'] = $customer_mount4_total[$i] ? $customer_mount4_total[$i] : 0;
			//存储员工基本信息
			$analysis_list1[$i]['user_id'] = $uvalue['user_id'];
			$analysis_list1[$i]['sector_id'] = $uvalue['sector_id'];
		}

		$wheres1['parent_company'] = array('eq',1);
		$wheres1['is_deleted'] = array('neq',1);
		$subcompany = $Sector->where($wheres1)->select();
			/*$subcompany_list:总办数组*/
		foreach ($subcompany as $skey => $svalue1) {
			$subcompany_list[$skey]= $svalue1['sector_id'];
		}

		$wheres2['parent_sector'] = array('in',$subcompany_list);
		$wheres2['is_deleted'] = array('neq',1);
		$zongjian = $Sector->where($wheres2)->select();
		/*$zongjian_list：总监数组*/
		foreach ($zongjian as $skey => $svalue2) {
			$zongjian_list[] = $svalue2['sector_id'];
		}
		$wheres3['parent_sector'] = array('in',$zongjian_list);
		$wheres3['is_deleted'] = array('neq',1);
		$jingli = $Sector->where($wheres3)->select();
		foreach ($jingli as $jlkey => $jlva) {
			$jingli_list[] = $jlva['sector_id'];
		}
		$maps0 = array();
        $maps0['parent_role'] = array('eq',$this->m_user['role_id']);
        $find_res = $Role ->field("role_id,parent_role")->where($maps0)->find();

	//用户登录判断
		//总监登录，部门统计
		if(in_array($this->m_user['sector_id'],$zongjian_list)){
			$ks = -1;
			for($key=1;$key<count($new_arr);$key++) {
				$ks++;
				foreach ($analysis_list1 as $ukey => $uvalue) {
					if($new_arr[$key] == $uvalue['sector_id']){
						$analysis_list2[$ks]['sector_id'] = $new_arr[$key];
						$pp['sector_id'] = array('eq',$new_arr[$key]);
						$sector_name[$ks] = M('sector')->where( $pp)->getField("sector_name");
						$analysis_list2[$ks]['nickname'] = $sector_name[$ks];
						$analysis_list2[$ks]['resou_num'] += $analysis_list1[$ukey]['resou_num'];
						$analysis_list2[$ks]['jiancang_ok'] += $analysis_list1[$ukey]['jiancang_ok'];
						$analysis_list2[$ks]['jiancang_no'] += $analysis_list1[$ukey]['jiancang_no'];
						$analysis_list2[$ks]['zhiying'] += $analysis_list1[$ukey]['zhiying'];
						$analysis_list2[$ks]['deal_mounts'] += $analysis_list1[$ukey]['deal_mounts'];
						if($analysis_list2[$ks]['resou_num']&&$analysis_list2[$ks]['deal_mounts'])
							$analysis_list2[$ks]['dealret'] = strval(round($analysis_list2[$ks]['deal_mounts']/$analysis_list2[$ks]['resou_num'],4)*100).'%';
						else $analysis_list2[$ks]['dealret'] = 0;
						$analysis_list2[$ks]['shilian'] += $analysis_list1[$ukey]['shilian'];
						$analysis_list2[$ks]['complain_num'] += $analysis_list1[$ukey]['complain_num'];
						$analysis_list2[$ks]['xindan'] += $analysis_list1[$ukey]['xindan'];
						$analysis_list2[$ks]['xindan_pay_total'] += $analysis_list1[$ukey]['xindan_pay_total'];
						$analysis_list2[$ks]['xufei_pay_total'] += $analysis_list1[$ukey]['xufei_pay_total'];
						$analysis_list2[$ks]['tuikuan_pay_total'] += $analysis_list1[$ukey]['tuikuan_pay_total'];
						$analysis_list2[$ks]['resou_total'] += $analysis_list1[$ukey]['resou_total'];
						$analysis_list2[$ks]['eval_mount_total'] += $analysis_list1[$ukey]['eval_mount_total'];
						if($analysis_list2[$ks]['resou_num'])
							$analysis_list2[$ks]['record_aver'] = intval($analysis_list2[$ks]['eval_mount_total']/$analysis_list2[$ks]['resou_num']);
						else $analysis_list2[$ks]['record_aver'] = 0;
						$analysis_list2[$ks]['customer_mount1_total'] += $analysis_list1[$ukey]['customer_mount1_total'];
						$analysis_list2[$ks]['customer_mount2_total'] += $analysis_list1[$ukey]['customer_mount2_total'];
						$analysis_list2[$ks]['customer_mount3_total'] += $analysis_list1[$ukey]['customer_mount3_total'];
						$analysis_list2[$ks]['customer_mount4_total'] += $analysis_list1[$ukey]['customer_mount4_total'];
						
					}
				}
			}
		//总办登录，部门统计
		}else if(in_array($this->m_user['sector_id'],$subcompany_list)){
			$wheres4['parent_sector'] = array('eq',$this->m_user['sector_id']);
			$wheres4['is_deleted'] = array('neq',1);
			$own_zongjian = $Sector->field('sector_id,sector_name') ->where($wheres4)->select(); //总办直接下属
			foreach ($own_zongjian as $zjkey => $zjval) {
				$own_zongjian_list[]['sector_id'] = $zjval['sector_id'];
			}
			$mapss['is_deleted'] = array('neq',1);
        	$array = $Sector->field("sector_id,sector_name,parent_sector") -> where($mapss)->select();
			$own_sector_arr = list_to_sector($array,$own_zongjian_list);
			foreach ($own_sector_arr as $arrkey => $arrval) {
				foreach ($analysis_list1 as $ukey => $uvalue) {
					if(in_array($uvalue['sector_id'],$arrval)){
						$analysis_list2[$arrkey]['sector_id'] = $arrval[0];
						$pp['sector_id'] = array('eq',$arrval[0]);
						$sector_name[$arrkey] = M('sector')->where( $pp)->getField("sector_name");
						$analysis_list2[$arrkey]['nickname'] = $sector_name[$arrkey];
						$analysis_list2[$arrkey]['resou_num'] += $analysis_list1[$ukey]['resou_num'];
						$analysis_list2[$arrkey]['jiancang_ok'] += $analysis_list1[$ukey]['jiancang_ok'];
						$analysis_list2[$arrkey]['jiancang_no'] += $analysis_list1[$ukey]['jiancang_no'];
						$analysis_list2[$arrkey]['zhiying'] += $analysis_list1[$ukey]['zhiying'];
						$analysis_list2[$arrkey]['deal_mounts'] += $analysis_list1[$ukey]['deal_mounts'];
						if($analysis_list2[$arrkey]['resou_num']&&$analysis_list2[$arrkey]['deal_mounts'])
							$analysis_list2[$arrkey]['dealret'] = strval(round($analysis_list2[$arrkey]['deal_mounts']/$analysis_list2[$arrkey]['resou_num'],4)*100).'%';
						else $analysis_list2[$arrkey]['dealret'] = 0;
						$analysis_list2[$arrkey]['shilian'] += $analysis_list1[$ukey]['shilian'];
						$analysis_list2[$arrkey]['complain_num'] += $analysis_list1[$ukey]['complain_num'];
						$analysis_list2[$arrkey]['xindan'] += $analysis_list1[$ukey]['xindan'];
						$analysis_list2[$arrkey]['xindan_pay_total'] += $analysis_list1[$ukey]['xindan_pay_total'];
						$analysis_list2[$arrkey]['xufei_pay_total'] += $analysis_list1[$ukey]['xufei_pay_total'];
						$analysis_list2[$arrkey]['tuikuan_pay_total'] += $analysis_list1[$ukey]['tuikuan_pay_total'];
						$analysis_list2[$arrkey]['resou_total'] += $analysis_list1[$ukey]['resou_total'];
						$analysis_list2[$arrkey]['eval_mount_total'] += $analysis_list1[$ukey]['eval_mount_total'];
						if($analysis_list2[$arrkey]['resou_num'])
							$analysis_list2[$arrkey]['record_aver'] = intval($analysis_list2[$arrkey]['eval_mount_total']/$analysis_list2[$arrkey]['resou_num']);
						else $analysis_list2[$arrkey]['record_aver'] = 0;
						$analysis_list2[$arrkey]['customer_mount1_total'] += $analysis_list1[$ukey]['customer_mount1_total'];
						$analysis_list2[$arrkey]['customer_mount2_total'] += $analysis_list1[$ukey]['customer_mount2_total'];
						$analysis_list2[$arrkey]['customer_mount3_total'] += $analysis_list1[$ukey]['customer_mount3_total'];
						$analysis_list2[$arrkey]['customer_mount4_total'] += $analysis_list1[$ukey]['customer_mount4_total'];
					}
				}
			}
		//经理/普通员工登录，部门统计
		}else if(in_array($this->m_user['sector_id'],$jingli_list)||empty($find_res)){
			$jingli_sector = $this->m_user['sector_id'];
			$jingli_sector_list[] = array('sector_id'=>$jingli_sector);
			foreach ($jingli_sector_list as $w => $wvalue) {
				foreach ($analysis_list1 as $ukey => $uvalue){
					if($uvalue['sector_id']==$wvalue['sector_id']){
						$analysis_list2[$w]['sector_id'] = $wvalue['sector_id'];
						$pp['sector_id'] = array('eq',$wvalue['sector_id']);
						$sector_name = M('sector')->where( $pp)->getField("sector_name");
						$analysis_list2[$w]['nickname'] = $sector_name;
						$analysis_list2[$w]['resou_num'] += $analysis_list1[$ukey]['resou_num'];
						$analysis_list2[$w]['jiancang_ok'] += $analysis_list1[$ukey]['jiancang_ok'];
						$analysis_list2[$w]['jiancang_no'] += $analysis_list1[$ukey]['jiancang_no'];
						$analysis_list2[$w]['zhiying'] += $analysis_list1[$ukey]['zhiying'];
						$analysis_list2[$w]['deal_mounts'] += $analysis_list1[$ukey]['deal_mounts'];
						if($analysis_list2[$w]['resou_num']&&$analysis_list2[$w]['deal_mounts'])
							$analysis_list2[$w]['dealret'] = strval(round($analysis_list2[$w]['deal_mounts']/$analysis_list2[$w]['resou_num'],4)*100).'%';
						else $analysis_list2[$w]['dealret'] = 0;
						$analysis_list2[$w]['shilian'] += $analysis_list1[$ukey]['shilian'];
						$analysis_list2[$w]['complain_num'] += $analysis_list1[$ukey]['complain_num'];
						$analysis_list2[$w]['xindan'] += $analysis_list1[$ukey]['xindan'];
						$analysis_list2[$w]['xindan_pay_total'] += $analysis_list1[$ukey]['xindan_pay_total'];
						$analysis_list2[$w]['xufei_pay_total'] += $analysis_list1[$ukey]['xufei_pay_total'];
						$analysis_list2[$w]['tuikuan_pay_total'] += $analysis_list1[$ukey]['tuikuan_pay_total'];
						$analysis_list2[$w]['resou_total'] += $analysis_list1[$ukey]['resou_total'];
						$analysis_list2[$w]['eval_mount_total'] += $analysis_list1[$ukey]['eval_mount_total'];
						if($analysis_list2[$w]['resou_num'])
							$analysis_list2[$w]['record_aver'] = intval($analysis_list2[$w]['eval_mount_total']/$analysis_list2[$w]['resou_num']);
						else $analysis_list2[$w]['record_aver'] = 0;
						$analysis_list2[$w]['customer_mount1_total'] += $analysis_list1[$ukey]['customer_mount1_total'];
						$analysis_list2[$w]['customer_mount2_total'] += $analysis_list1[$ukey]['customer_mount2_total'];
						$analysis_list2[$w]['customer_mount3_total'] += $analysis_list1[$ukey]['customer_mount3_total'];
						$analysis_list2[$w]['customer_mount4_total'] += $analysis_list1[$ukey]['customer_mount4_total'];
					}
				}
			}
		//admin登录，部门统计
		}else if($this->m_user['user_id']==1){
			foreach ($subcompany as $skey => $svalue1) {
				$subcompany_list[$skey]['sector_id'] = $svalue1['sector_id'];  //各分公司总办部门subcompany_list:30 31 32 53 62 63 65
			}
			foreach ($subcompany_list as $skey => $sval) {
				$subcompany_list1[$sval] = $sval;
			}
			$map1['is_deleted'] = array('neq',1);
			foreach ($subcompany_list1 as $lkey => $lval) {
				$map1['parent_sector'] = array('eq',$lval);
				$sector_sub[$lkey] = $Sector->field("sector_id,sector_name,parent_sector") -> where($map1)->select();
			}
			foreach ($jingli as $jlkey => $jlval) {
				foreach ($analysis_list1 as $ukey => $uvall) {
					if($uvall['sector_id']==$jlval['sector_id']){
						$analysis_list2[$jlkey]['sector_id'] = $jlval['sector_id'];
						$pp['sector_id'] = array('eq',$jlval['sector_id']);
						$sector_name[$jlkey] = M('sector')->where( $pp)->getField("sector_name");
						$parent_sector_name[$jlkey] = M('sector')->where( $pp)->getField("parent_sector");
						$analysis_list2[$jlkey]['nickname'] = $sector_name[$jlkey];
						$analysis_list2[$jlkey]['parent_sector'] = $parent_sector_name[$jlkey];
						$analysis_list2[$jlkey]['resou_num'] += $uvall['resou_num'];
						$analysis_list2[$jlkey]['jiancang_ok'] += $uvall['jiancang_ok'];
						$analysis_list2[$jlkey]['jiancang_no'] += $uvall['jiancang_no'];
						$analysis_list2[$jlkey]['zhiying'] += $uvall['zhiying'];
						$analysis_list2[$jlkey]['deal_mounts'] += $uvall['deal_mounts'];
						if($analysis_list2[$jlkey]['resou_num']&&$analysis_list2[$jlkey]['deal_mounts'])
							$analysis_list2[$jlkey]['dealret'] = strval(round($analysis_list2[$jlkey]['deal_mounts']/$analysis_list2[$jlkey]['resou_num'],4)*100).'%';
						else $analysis_list2[$jlkey]['dealret'] = 0;
						$analysis_list2[$jlkey]['shilian'] += $uvall['shilian'];
						$analysis_list2[$jlkey]['complain_num'] += $uvall['complain_num'];
						$analysis_list2[$jlkey]['xindan'] += $uvall['xindan'];
						$analysis_list2[$jlkey]['xindan_pay_total'] += $uvall['xindan_pay_total'];
						$analysis_list2[$jlkey]['xufei_pay_total'] += $uvall['xufei_pay_total'];
						$analysis_list2[$jlkey]['tuikuan_pay_total'] += $uvall['tuikuan_pay_total'];
						$analysis_list2[$jlkey]['resou_total'] += $uvall['resou_total'];
						$analysis_list2[$jlkey]['eval_mount_total'] += $uvall['eval_mount_total'];
						if($analysis_list2[$jlkey]['resou_num'])
							$analysis_list2[$jlkey]['record_aver'] = intval($analysis_list2[$jlkey]['eval_mount_total']/$analysis_list2[$jlkey]['resou_num']);
						else $analysis_list2[$jlkey]['record_aver'] = 0;
						$analysis_list2[$jlkey]['customer_mount1_total'] += $uvall['customer_mount1_total'];
						$analysis_list2[$jlkey]['customer_mount2_total'] += $uvall['customer_mount2_total'];
						$analysis_list2[$jlkey]['customer_mount3_total'] += $uvall['customer_mount3_total'];
						$analysis_list2[$jlkey]['customer_mount4_total'] += $uvall['customer_mount4_total'];
					}
				}
			}
			foreach ($zongjian as $zjkey => $zjval) {
				foreach ($analysis_list2 as $ukeys => $uvals) {
					if($zjval['sector_id']==$uvals['parent_sector']){
						$analysis_list3[$zjkey]['sector_id'] = $zjval['sector_id'];
						$pp['sector_id'] = array('eq',$zjval['sector_id']);
						$sector_name[$zjkey] = M('sector')->where( $pp)->getField("sector_name");
						$parent_sector_name[$zjkey] = M('sector')->where( $pp)->getField("parent_sector");
						$analysis_list3[$zjkey]['nickname'] = $sector_name[$zjkey];
						$analysis_list3[$zjkey]['parent_sector'] = $parent_sector_name[$zjkey];
						$analysis_list3[$zjkey]['resou_num'] += $uvals['resou_num'];
						$analysis_list3[$zjkey]['jiancang_ok'] += $uvals['jiancang_ok'];
						$analysis_list3[$zjkey]['jiancang_no'] += $uvals['jiancang_no'];
						$analysis_list3[$zjkey]['zhiying'] += $uvals['zhiying'];
						$analysis_list3[$zjkey]['deal_mounts'] += $uvals['deal_mounts'];
						if($analysis_list3[$zjkey]['resou_num']&&$analysis_list3[$zjkey]['deal_mounts'])
							$analysis_list3[$zjkey]['dealret'] = strval(round($analysis_list3[$zjkey]['deal_mounts']/$analysis_list3[$zjkey]['resou_num'],4)*100).'%';
						else $analysis_list3[$zjkey]['dealret'] = 0;
						$analysis_list3[$zjkey]['shilian'] += $uvals['shilian'];
						$analysis_list3[$zjkey]['complain_num'] += $uvals['complain_num'];
						$analysis_list3[$zjkey]['xindan'] += $uvals['xindan'];
						$analysis_list3[$zjkey]['xindan_pay_total'] += $uvals['xindan_pay_total'];
						$analysis_list3[$zjkey]['xufei_pay_total'] += $uvals['xufei_pay_total'];
						$analysis_list3[$zjkey]['tuikuan_pay_total'] += $uvals['tuikuan_pay_total'];
						$analysis_list3[$zjkey]['resou_total'] += $uvals['resou_total'];
						$analysis_list3[$zjkey]['eval_mount_total'] += $uvals['eval_mount_total'];
						if($analysis_list3[$zjkey]['resou_num'])
							$analysis_list3[$zjkey]['record_aver'] = intval($analysis_list3[$zjkey]['eval_mount_total']/$analysis_list3[$zjkey]['resou_num']);
						else $analysis_list3[$zjkey]['record_aver'] = 0;
						$analysis_list3[$zjkey]['customer_mount1_total'] += $uvals['customer_mount1_total'];
						$analysis_list3[$zjkey]['customer_mount2_total'] += $uvals['customer_mount2_total'];
						$analysis_list3[$zjkey]['customer_mount3_total'] += $uvals['customer_mount3_total'];
						$analysis_list3[$zjkey]['customer_mount4_total'] += $uvals['customer_mount4_total'];
					}
				}
			}
			//总监以外的资源总和
			foreach ($analysis_list3 as $ukk => $uval) {
				foreach ($analysis_list1 as $ukey => $uvall) {
					if($uval['sector_id']==$uvall['sector_id']){
						$analysis_list3[$ukk]['resou_num'] += $uvall['resou_num'];
						$analysis_list3[$ukk]['jiancang_ok'] += $uvall['jiancang_ok'];
						$analysis_list3[$ukk]['jiancang_no'] += $uvall['jiancang_no'];
						$analysis_list3[$ukk]['zhiying'] += $uvall['zhiying'];
						$analysis_list3[$ukk]['deal_mounts'] += $uvall['deal_mounts'];
						if($analysis_list3[$ukk]['resou_num']&&$analysis_list3[$ukk]['deal_mounts'])
							$analysis_list3[$ukk]['dealret'] = strval(round($analysis_list3[$ukk]['deal_mounts']/$analysis_list3[$ukk]['resou_num'],4)*100).'%';
						else $analysis_list3[$ukk]['dealret'] = 0;
						$analysis_list3[$ukk]['shilian'] += $uvall['shilian'];
						$analysis_list3[$ukk]['complain_num'] += $uvall['complain_num'];
						$analysis_list3[$ukk]['xindan'] += $uvall['xindan'];
						$analysis_list3[$ukk]['xindan_pay_total'] += $uvall['xindan_pay_total'];
						$analysis_list3[$ukk]['xufei_pay_total'] += $uvall['xufei_pay_total'];
						$analysis_list3[$ukk]['tuikuan_pay_total'] += $uvall['tuikuan_pay_total'];
						$analysis_list3[$ukk]['resou_total'] += $uvall['resou_total'];
						$analysis_list3[$ukk]['eval_mount_total'] += $uvall['eval_mount_total'];
						if($analysis_list3[$ukk]['resou_num'])
							$analysis_list3[$ukk]['record_aver'] = intval($analysis_list3[$ukk]['eval_mount_total']/$analysis_list3[$ukk]['resou_num']);
						else $analysis_list3[$ukk]['record_aver'] = 0;
						$analysis_list3[$ukk]['customer_mount1_total'] += $uvall['customer_mount1_total'];
						$analysis_list3[$ukk]['customer_mount2_total'] += $uvall['customer_mount2_total'];
						$analysis_list3[$ukk]['customer_mount3_total'] += $uvall['customer_mount3_total'];
						$analysis_list3[$ukk]['customer_mount4_total'] += $uvall['customer_mount4_total'];
					}
				}
			}
			foreach ($subcompany as $skey => $svalue1){
				foreach ($analysis_list3 as $ukey => $uvaluess) {
					if($svalue1['sector_id']==$uvaluess['parent_sector']){
						$analysis_list4[$skey]['sector_id'] = $svalue1['sector_id'];
						$pp['sector_id'] = array('eq',$svalue1['sector_id']);
						$sector_name[$skey] = M('sector')->where( $pp)->getField("sector_name");
						$analysis_list4[$skey]['nickname'] = $sector_name[$skey];
						$analysis_list4[$skey]['resou_num'] += $uvaluess['resou_num'];
						$analysis_list4[$skey]['jiancang_ok'] += $uvaluess['jiancang_ok'];
						$analysis_list4[$skey]['jiancang_no'] += $uvaluess['jiancang_no'];
						$analysis_list4[$skey]['zhiying'] += $uvaluess['zhiying'];
						$analysis_list4[$skey]['deal_mounts'] += $uvaluess['deal_mounts'];
						if($analysis_list4[$skey]['resou_num']&&$analysis_list4[$skey]['deal_mounts'])
							$analysis_list4[$skey]['dealret'] = strval(round($analysis_list4[$skey]['deal_mounts']/$analysis_list4[$skey]['resou_num'],4)*100).'%';
						else $analysis_list4[$skey]['dealret'] = 0;
						$analysis_list4[$skey]['shilian'] += $uvaluess['shilian'];
						$analysis_list4[$skey]['complain_num'] += $uvaluess['complain_num'];
						$analysis_list4[$skey]['xindan'] += $uvaluess['xindan'];
						$analysis_list4[$skey]['xindan_pay_total'] += $uvaluess['xindan_pay_total'];
						$analysis_list4[$skey]['xufei_pay_total'] += $uvaluess['xufei_pay_total'];
						$analysis_list4[$skey]['tuikuan_pay_total'] += $uvaluess['tuikuan_pay_total'];
						$analysis_list4[$skey]['resou_total'] += $uvaluess['resou_total'];
						$analysis_list4[$skey]['eval_mount_total'] += $uvaluess['eval_mount_total'];
						if($analysis_list4[$skey]['resou_num'])
							$analysis_list4[$skey]['record_aver'] = intval($analysis_list4[$skey]['eval_mount_total']/$analysis_list4[$skey]['resou_num']);
						else $analysis_list4[$skey]['record_aver'] = 0;
						$analysis_list4[$skey]['customer_mount1_total'] += $uvaluess['customer_mount1_total'];
						$analysis_list4[$skey]['customer_mount2_total'] += $uvaluess['customer_mount2_total'];
						$analysis_list4[$skey]['customer_mount3_total'] += $uvaluess['customer_mount3_total'];
						$analysis_list4[$skey]['customer_mount4_total'] += $uvaluess['customer_mount4_total'];
					}
				}
			}
			$analysis_list2 = $analysis_list4;
		}
		//普通员工登录，个人统计
		if(empty($find_res)){
			$own_userid = $this->m_user['user_id'];
			$userid_list = array('user_id'=>$own_userid);
			foreach ($userid_list as $uk => $uvals) {
				foreach ($analysis_list1 as $ukey => $uvalue) {
					if($uvalue['user_id']==$uvals){
						$analysis_list5[0]['sector_id'] = $uvalue['sector_id'];
						$analysis_list5[0]['nickname'] = $uvalue['nickname'];
						$analysis_list5[0]['resou_num'] = $uvalue['resou_num'];
						$analysis_list5[0]['jiancang_ok'] = $uvalue['jiancang_ok'];
						$analysis_list5[0]['jiancang_no'] = $uvalue['jiancang_no'];
						$analysis_list5[0]['zhiying'] = $uvalue['zhiying'];
						$analysis_list5[0]['deal_mounts'] = $uvalue['deal_mounts'];
						if($analysis_list5[0]['resou_num']&&$analysis_list5[0]['deal_mounts'])
							$analysis_list5[0]['dealret'] = strval(round($analysis_list5[0]['deal_mounts']/$analysis_list5[0]['resou_num'],4)*100).'%';
						else $analysis_list5[0]['dealret'] = 0;
						$analysis_list5[0]['shilian'] = $uvalue['shilian'];
						$analysis_list5[0]['complain_num'] = $uvalue['complain_num'];
						$analysis_list5[0]['xindan'] += $uvalue['xindan'];
						$analysis_list5[0]['xindan_pay_total'] = $uvalue['xindan_pay_total'];
						$analysis_list5[0]['xufei_pay_total'] = $uvalue['xufei_pay_total'];
						$analysis_list5[0]['tuikuan_pay_total'] = $uvalue['tuikuan_pay_total'];
						$analysis_list5[0]['resou_total'] = $uvalue['resou_total'];
						$analysis_list5[0]['eval_mount_total'] = $uvalue['eval_mount_total'];
						if($analysis_list5[0]['resou_num'])
							$analysis_list5[0]['record_aver'] = intval($analysis_list5[0]['eval_mount_total']/$analysis_list5[0]['resou_num']);
						else $analysis_list5[0]['record_aver'] = 0;
						$analysis_list5[0]['customer_mount1_total'] = $uvalue['customer_mount1_total'];
						$analysis_list5[0]['customer_mount2_total'] = $uvalue['customer_mount2_total'];
						$analysis_list5[0]['customer_mount3_total'] = $uvalue['customer_mount3_total'];
						$analysis_list5[0]['customer_mount4_total'] = $uvalue['customer_mount4_total'];
					}
				}
			}
			$analysis_list1 = $analysis_list5;
		}
		//分页显示+查看方式切换
		if($ids == 1){			//按照部门
	        $cnt2 = count($analysis_list2);
			$Page = new \Common\ORG\Page( $cnt2, $psize );
			$num1 = -1;
	        if(($Page->nowPage)*$psize<=$cnt2){
	            for($k=($Page->nowPage-1)*$psize;$k<($Page->nowPage)*$psize;$k++){
	                $num1++;
	                $analysis_lists2[$num1] = $analysis_list2[$k];
	            }
	        }else{
	            for($k=($Page->nowPage-1)*$psize;$k<$cnt2;$k++){
	                $num1++;
	                $analysis_lists2[$num1] = $analysis_list2[$k];
	            }
	        }
	        $res['analysis_list'] = $analysis_lists2;
		}else if($ids == 2 && empty($find_res)){ //普通员工个人数据
			$res['analysis_list'] = $analysis_list1;
	        $res['pall'] =  1;
	        $res['p'] = 1;
	        $res['psize'] = 20;
			e_json( $res );
		}
		else if($ids == 2){		//按照个人
			$cnt1 = count($analysis_list1);
			$Page = new \Common\ORG\Page( $cnt1, $psize );
			$num2 = -1;
	        if(($Page->nowPage)*$psize<=$cnt1){
	            for($k=($Page->nowPage-1)*$psize;$k<($Page->nowPage)*$psize;$k++){
	                $num2++;
	                $analysis_lists1[$num2] = $analysis_list1[$k];
	            }
	        }else{
	            for($k=($Page->nowPage-1)*$psize;$k<$cnt1;$k++){
	                $num2++;
	                $analysis_lists1[$num2] = $analysis_list1[$k];
	            }
	        }
	        $res['analysis_list'] = $analysis_lists1;
		}
		$res['pall'] =  $Page->getTotalPages();
        $res['p'] = $Page->nowPage;
        $res['psize'] = $psize;
		e_json( $res );
	}
}